package com.wensi.view

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.widget.ImageView
import android.widget.LinearLayout
import com.wensi.lib_base.R

/**
 * 引导翻页的布局
 */
class PointLayoutView : LinearLayout {
    private var list = ArrayList<ImageView>()
    constructor(context: Context?) : super(context) { initLayout()}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs){ initLayout()}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){ initLayout()}

    private fun initLayout() {
        orientation = HORIZONTAL
        gravity = Gravity.CENTER
    }
    // 设置页面数量
    fun setPointSize(size: Int) {
        if (list.size > 0) {
            list.clear()
        }
        for(i in 0 until size) {
            val img = ImageView(context)
            addView(img)
            list.add(img)
        }
        // 默认第一页
        setChecked(0)
    }
    // 设置选中
    fun setChecked(index: Int) {
        if(index <= list.size) {
            list.forEachIndexed { i, imageView ->
                val  resourceId = if (i == index) R.drawable.img_app_manager_point_p else R.drawable.img_app_manager_point
                imageView.setImageResource(resourceId)
            }
        }
    }
}